//-----------------------------------------------------------------------
// <copyright file="AddBinding.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Adds a new host/ip binding to the specified web site.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using Microsoft.Sdc.Tasks.Configuration.Web;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Adds a new host/ip binding to the specified web site
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.WebSite.AddBinding 
    ///     Port="port"
    ///     HostName="hostname" 
    ///     Description="description"
    ///     Identifier="identifier"
    ///     IPAddress="ipAddress"
    ///     Port="port" 
    ///     Secure="secure"
    ///     MachineName="machineName"
    /// />]]></code>
    /// <para>where:</para>
    /// <para><i>description</i></para>
    /// <para>Web site description to apply binding to</para>
    /// <para><i>identifier</i></para>
    /// <para>Web site identifier to apply binding to</para>
    /// <para><i>ipaddress</i></para>
    /// <para>IP address to listen on. Defaults to any </para>
    /// <para><i>port (Required)</i></para>
    /// <para>Port number to bind to</para>
    /// <para><i>hostname</i></para>
    /// <para>Host header to bind to (only valid for non-SSL sites)</para>
    /// <para><i>Secure (optional)</i></para>
    /// <para>true if this is a secure SSL binding. False for non-secure bindings</para>
    /// <para><i>machineName</i></para>
    /// <para>Machine to operate on(defaults to localhost)</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.WebSite.AddBinding
    ///             Description="mytestsite"
    ///             IPAddress="10.0.0.1"
    ///             Port="80"
    ///             HostName="hostname.example" 
    ///             Secure="false"/>
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class AddBinding : TaskBase
    {
        private string description;
        private string ipAddress = String.Empty;
        private string port;
        private bool secure = false;
        private string hostName;
        private string identifier;
        private string machineName = "localhost";

        /// <summary>
        /// Machine to operate on
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// Gets or sets the Description of web site to apply binding to
        /// </summary>
        /// <value>A valid web site description</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Identifier">Identifier</seealso> 
        /// for the site you wish to apply the binding to</remarks>
        public string Description
        {
            get { return (this.description ?? String.Empty); }
            set { this.description = value; }
        }

        /// <summary>
        /// Gets or sets the IP address to bind to
        /// </summary>
        /// <value>A valid IP address configured on the host machine</value>
        public string IPAddress
        {
            get { return (this.ipAddress ?? String.Empty); }
            set { this.ipAddress = value; }
        }

        /// <summary>
        /// Gets or sets the Port number to bind to
        /// </summary>
        /// <value>A valid port number to bind the web site to</value>
        [Required]
        public string Port
        {
            get { return (this.port ?? String.Empty); }
            set { this.port = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the binding is secure
        /// </summary>
        /// <value>bool</value>
        [Required]
        public bool Secure
        {
            get { return this.secure; }
            set { this.secure = value; }
        }

        /// <summary>
        /// Gets or sets the Host header to bind the web site to
        /// </summary>
        /// <value>A valid host name</value>
        public string HostName
        {
            get { return (this.hostName ?? String.Empty); }
            set { this.hostName = value; }
        }

        /// <summary>
        /// Gets or sets the web site idenfitier to apply binding to
        /// </summary>
        /// <value>A valid web site identifier</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Description">Description</seealso> 
        /// for the site you wish to delete</remarks>
        public string Identifier
        {
            get { return (this.identifier ?? String.Empty); }
            set { this.identifier = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            int id = Utilities.CheckWebSiteDescriptionOrIdentifier(this.Description, this.Identifier, "AddBinding", this.MachineName);
            if (WebSite.Exists(this.MachineName, id))
            {
                Microsoft.Sdc.Tasks.Configuration.Web.WebSite web = WebSite.Load(this.MachineName, id);
                web.AddBinding(this.IPAddress, this.Port, this.HostName, this.Secure);
                web.Save();
            }
        }
    }
}